package com.dream.order.service.impl;

import java.util.Date;
import java.util.List;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dream.app.service.OrgService;
import com.dream.dao.order.OnlineOrderDAO;
import com.dream.dao.order.OrderCustomerDAO;
import com.dream.dao.order.OrderCustomerProductDAO;
import com.dream.master.service.ProductCategoryService;
import com.dream.model.app.AppAbstractUser;
import com.dream.model.app.AppOrg;
import com.dream.model.master.Product;
import com.dream.model.master.ProductCategory;
import com.dream.model.order.OnlineOrder;
import com.dream.model.order.OnlineOrder.OrderStatus;
import com.dream.model.order.OrderCustomer;
import com.dream.model.order.OrderCustomerProduct;
import com.dream.order.service.OrderService;
import com.dream.utils.date.DateParseUtils;
import com.dream.utils.hibernate.PagingVO;
import com.dream.vo.api.ApiOrderUpdate;
import com.dream.vo.order.CustomerProductVO;
import com.dream.vo.order.CustomerVO;
import com.dream.vo.order.OrderQueryVO;
import com.dream.vo.order.OrderVO;

@Service("OrderService")
public class OrderServiceImpl implements OrderService {

	@Autowired
	private  OnlineOrderDAO onLineOrderDAO;
	
	@Autowired
	private OrgService orgService;
	
	@Autowired
	private OrderCustomerDAO orderCustomerDAO;
	
	@Autowired
	private OrderCustomerProductDAO orderProductDAO;
	
	@Autowired
	private OnlineOrderDAO onlineOrderDAO;
	
	@Autowired
	private ProductCategoryService productCategoryService;
	
	@Transactional
	@Override
	public PagingVO query(OrderQueryVO vo, PagingVO page, AppAbstractUser user) {
		
		List<Long> leafOrgs = orgService.getLeafOrgs(vo.getFirstOrg(), vo.getSecondOrg());
		vo.setOrgs(leafOrgs);
		page = onLineOrderDAO.query(vo, page);
		
		@SuppressWarnings("unchecked")
		List<OnlineOrder> li = (List<OnlineOrder>) page.getDetails();
		if(CollectionUtils.isNotEmpty(li)){
			for(OnlineOrder item : li){
				if(item != null && item.getOrderProduct() != null){
					setOrg(item.getOrderProduct().getCustomer());
				}
			}
		}
		return page;
	}
	
	@Transactional
	private void setOrg(OrderCustomer customer){
		if(customer != null){
			List<AppOrg> orgs = orgService.getParent(customer.getOrgId());
			customer.initOrgs(orgs);
		}
		
	}

	@Override
	@Transactional
	public OrderCustomer updateCustomer(CustomerVO vo, AppAbstractUser user) {
		
		if(vo.getUserId() == null){
			
			OrderCustomer cus = new OrderCustomer();
			vo.populate(cus);
			cus.setCreateUser(user.getId());
			cus.setCreateTime(new Date());
			orderCustomerDAO.save(cus);
			return cus;
		}
		
		OrderCustomer customer = orderCustomerDAO.selectById(vo.getUserId());
		vo.populate(customer);
		customer.setUpdateUser(user.getId());
		customer.setUpdateTime(new Date());
		orderCustomerDAO.saveOrUpdate(customer);;
		return customer;
		
	}

	@Override
	@Transactional
	public OrderCustomerProduct updateCustomerProduct(CustomerProductVO vo, AppAbstractUser user) {
		
		if(StringUtils.isBlank(vo.getId())){
			
			OrderCustomerProduct product = new OrderCustomerProduct();
			vo.populate(product);
			product.setCreateUser(user.getId());
			product.setCreateTime(new Date());
			orderProductDAO.save(product);
			return product;
		}
		
		OrderCustomerProduct product = orderProductDAO.selectById(vo.getId());
		vo.populate(product);
		product.setUpdateUser(user.getId());
		product.setUpdateTime(new Date());
		orderProductDAO.saveOrUpdate(product);
		return product;
	}

	@Override
	@Transactional
	public OnlineOrder updateCustomerOrder(OrderVO vo, AppAbstractUser user) {
		
		if(vo.getId() == null){
			
			OnlineOrder order = new OnlineOrder();
			vo.populate(order);
			order.setCreateUser(user.getId());
			order.setCreateTime(new Date());
			onlineOrderDAO.save(order);
			return order;
		}
		
		OnlineOrder order = onlineOrderDAO.selectById(vo.getId());
		vo.populate(order);
		order.setUpdateUser(user.getId());
		order.setUpdateTime(new Date());
		onlineOrderDAO.saveOrUpdate(order);
		return order;
		
	}

	@Override
	@Transactional
	public void dispatchOrder(Long id, String shopId, AppAbstractUser user) {
		
		OnlineOrder order = onlineOrderDAO.selectById(id);
		order.setUpdateUser(user.getId());
		order.setUpdateTime(new Date());
		
		order.setStatus(OrderStatus.order_dispatch);
		order.setDispatchDate(new Date());
		order.setDispatchor(user.getId());
		order.setHandleShop(shopId);
		
		onlineOrderDAO.saveOrUpdate(order);
	}

	@Override
	@Transactional
	public void acceptOrder(Long id, AppAbstractUser user) {
		
		OnlineOrder order = onlineOrderDAO.selectById(id);
		order.setUpdateUser(user.getId());
		order.setUpdateTime(new Date());
		
		order.setStatus(OrderStatus.order_accept);
		order.setAcceptUser(user.getId());
		order.setAccpetDate(new Date());
		onlineOrderDAO.saveOrUpdate(order);
	}

	@Override
	@Transactional
	public OrderCustomer searchCustomer(String name, String mobile) {
		
		 OrderCustomer customer = orderCustomerDAO.searchCustomer(name, mobile);
		 setOrg(customer);
		 return customer;
	}

	@Override
	@Transactional
	public OrderCustomerProduct searchProduct(String productName, Long customerId) {
		
		OrderCustomerProduct cproduct = orderProductDAO.searchProduct(productName, customerId);
		if(cproduct != null){
			Product product = cproduct.getProduct();
			setProductCat(product);
		}
		return cproduct;
	}
	
	private void setProductCat(Product product){
		if(product != null){
			List<ProductCategory> cats = productCategoryService.getParent(product.getCategoryId());
			product.initCats(cats);
		}
	}

	@Override
	@Transactional
	public OnlineOrder getDetail(Long id) {
		OnlineOrder order = onlineOrderDAO.getById(id);
		if(order != null){
			setProductCat(order.getOrderProduct().getProduct());
			this.setOrg(order.getOrderProduct().getCustomer());
		}
		return order;
	}

	@Override
	@Transactional
	public void updateOrder(ApiOrderUpdate vo, AppAbstractUser user) {
		
		OnlineOrder order = onlineOrderDAO.getById(vo.getId());
		order.setUpdateUser(user.getId());
		order.setUpdateTime(new Date());
		order.setStatus(OrderStatus.order_fixed);
		
		OrderCustomerProduct product = order.getOrderProduct();
		product.setBarCode(vo.getProBarcode());
		product.setMandatoryNo(vo.getProMandatoryNo());
		product.setPurchaseDate(DateParseUtils.parseDate(vo.getProPurchaseDate()));
		product.setRecepitNo(vo.getProRecepitNo());
		product.setProductId(vo.getProductId());
		
		orderProductDAO.saveOrUpdate(product);
		onlineOrderDAO.saveOrUpdate(order);
	}
	
	
}
	

